package br.ufc.ck0017.semantic.model;

import br.ufc.ck0017.syntaxtree.Type;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class SemanticMethod {
    private final Type type;
    private Map<String, SemanticVariable> parameters;
    private Map<String, SemanticVariable> variables;
    private String name;

    public SemanticMethod(Type type) {
        this.type = type;

        parameters = new HashMap<>();
        variables = new HashMap<>();
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public SemanticVariable getVar(String name) {
        return variables.get(name);
    }

    public SemanticVariable getArg(String name) {
        return parameters.get(name);
    }

    public List<Type> getArgsTypes() {
        List<Type> types = new ArrayList<>();

        for (String name : parameters.keySet()) {
            SemanticVariable arg = parameters.get(name);
            types.add(arg.getType());
        }

        return types;
    }

    public Type getType() {
        return type;
    }

    public void addParameter(String name, SemanticVariable parameter) throws IllegalArgumentException {
        if (parameters.containsKey(name)) {
            throw new IllegalArgumentException();
        }

        parameters.put(name, parameter);
    }

    public void addVar(String name, SemanticVariable var) throws IllegalArgumentException {
        if (variables.containsKey(name)) {
            throw new IllegalArgumentException();
        }

        variables.put(name, var);
    }

    public int getParametersSize() {
        return parameters.size();
    }

    @Override
    public String toString() {
        return "SemanticMethod{" +
                "type='" + type + '\'' +
                ", parameters=" + parameters +
                ", variables=" + variables +
                '}';
    }
}
